package com.yanqu.road.server.manger.union.huaben;

import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.entity.union.huaben.UnionHuaBenServerGroup;
import com.yanqu.road.pb.union.cross.UnionHuaBenProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.UnionHuaBenServerGroupMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.plugin.ManagerReloadListener;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.Map;

public class UnionHuaBenMgr extends TempMgr {

    @Override
    public boolean init() throws Exception {
        UnionHuaBenServerGroupMgr.addReloadListener(new ManagerReloadListener() {
            @Override
            public void notifyReload() {
                syncGroup();
            }
        });
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static UnionHuaBenProto.UnionHuaBenGroupSyncMsg.Builder buildHuaBenGroup(UnionHuaBenServerGroup serverGroup) {
        UnionHuaBenProto.UnionHuaBenGroupSyncMsg.Builder syncMsg = UnionHuaBenProto.UnionHuaBenGroupSyncMsg.newBuilder();
        if(serverGroup == null){
            syncMsg.setCrossOpen(false);
        }else {
            syncMsg.setCrossOpen(serverGroup.getState() == 1);
            syncMsg.setGroupId(serverGroup.getGroupId());
            syncMsg.addAllServerList(new ArrayList<>(serverGroup.getServerList()));
        }
        return syncMsg;
    }

    private static void syncGroup() {
        UnionHuaBenServerGroup serverGroup = UnionHuaBenServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
        for (Map.Entry<String, CrossUnionInfo> entry : CrossUnionMgr.getUnionInfoMap().entrySet()) {
            CrossUnionInfo unionInfo = entry.getValue();
            Map<Long, CrossUnionMember> map = CrossUnionMgr.getUnionMemberMap(unionInfo.getUnionUid());
            if(map != null){
                for (Map.Entry<Long, CrossUnionMember> memberEntry : map.entrySet()) {
                    if(memberEntry.getValue().getServerId() == GameServer.getInstance().getServerId()){
                        GamePlayer player = GamePlayerMgr.getOnlinePlayer(memberEntry.getValue().getUserId());
                        if(player != null) {
                            UnionHuaBenProto.UnionHuaBenGroupSyncMsg.Builder syncMsg = buildHuaBenGroup(serverGroup);
                            player.sendPacket(Protocol.U_CROSS_UNION_HUA_BEN_GROUP_SYNC, syncMsg);
                        }
                    }
                }
            }
        }

    }
}
